
<?php

class Sql
{
    /**
     * @var array
     */
    private $fields = [];

    /**
     * @var array
     */
    private $from = [];

    /**
     * @var array
     */
    private $where = [];

    public function select(array $fields): Sql
    {
        $this->fields = $fields;

        return $this;
    }

    public function from(string $table, string $alias): Sql
    {
        $this->from[] = $table.' AS '.$alias;

        return $this;
    }

    public function where(string $condition): Sql
    {
        $this->where[] = $condition;

        return $this;
    }

    public function __toString(): string
    {
        return sprintf(
            'SELECT %s FROM %s WHERE %s',
            join(', ', $this->fields),
            join(', ', $this->from),
            join(' AND ', $this->where)
        );
    }
}
$query = (new Sql())
    ->select(['foo', 'bar'])
    ->from('foobar', 'f')
    ->where('f.bar = ?');
print_r($query);

/*
 * 流接口模式
 具体用于
1、ORM数据处理和链式查询
2、读取日志文件，根据特定规则筛选出重要的日志条目，进行格式化和存储
3、读取文件内容、逐行处理、进行数据提取或转换，然后写入到另一个文件
工作流管理
 */